// map.js
import QQMAP from '../../../utils/qqmap'
Page({
  data: {
    // latitude: 40.060539,
    // longitude: 116.343847,
    latitude: 30.59982,
    longitude: 114.3096,
    scale: 18,
    markers: [
      {
        id: 1,
        latitude: 30.69982,
        longitude: 114.4096,
        width: 24,
        height: 30,
      },
      {
        id: 2,
        latitude: 30.59982,
        longitude: 114.3096,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40,
      }],
    polyline: [],
    "houseInfo": "",
    "id": "",
    "houseId": "",
    "repairItemName": "",
    "repairItemId": "",
    "mobile": "",
    "appointment ": "2022-10-01",
    "description ": "",
    "attachment": [],
    "status": 0
  },
  getPolyline() {
    QQMAP.direction({
      mode: 'walking',
      from: '30.59982,114.3096',
      to: '30.69982,114.4096',
      success: (res) => {
        console.log(res);
        var ret = res;
        var coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (var i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl)
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        this.setData({
          latitude: pl[0].latitude,
          longitude: pl[0].longitude,
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 2
          }]
        })
      }
    })
  },

  async onLoad({ id }) {
    // 根据id拿到报修详情
    const res = await wx.$http.get('/repair/' + id)
    console.log('详情页', res)
    this.setData({ ...res })

    this.getPolyline()
  }
})
